package design.responsibility.demo02;

import java.util.Optional;
import java.util.function.Predicate;

/**
 * <p>
 * 职员抽象处理类
 * </P>
 *
 * @author dingwen
 * @date 2021.10.15 11:44
 */
public abstract class AbstractClerk {
    /**
     * 下一级别处理
     */
    private AbstractClerk nextClerk;

    /**
     * 夹头的人
     */
    protected String handlerPerson;

    public void setNextClerk(AbstractClerk nextClerk) {
        this.nextClerk = nextClerk;
    }

    /**
     * 得到的借款的范围
     *
     * @return double
     */
    protected abstract double getRange();


    /**
     * 批准
     */
    protected void approval(MoneyRequest request) {
        System.out.println(this.handlerPerson + "已经同意，金额" + request.getMoney());
    }


    public void handler(MoneyRequest request) {
        Predicate<Double> doublePredicate = money -> money >= request.getMoney();
        if (doublePredicate.test(this.getRange())) {
            this.approval(request);
        } else {
            Optional.ofNullable(nextClerk)
                    .orElseThrow(() -> new RuntimeException("没有找到合适的处理程序~"))
                    .handler(request);
        }

    }

}
